package com.loongtech.bi.action.system;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.BiServerManager;
import com.loongtech.core.util.Utils;

/**
 * 区服配置管理
 * 
 * @author marin
 * @date 2015-09-29
 */
@Controller("serverConfigAction")
@Scope("view")
public class ServerConfigAction {
	@Autowired
	private BiPlatformManager biPlatformManager;
	@Autowired
	private BiServerManager biServerManager;
	
	private EntityBiServer entity;
	private List<EntityBiServer> list;
	
	private List<SelectObject> selected;

	private Map<String, String> allItmes;

	@Autowired
	public void init() {
		initData();
	}
	
	private void initData() {
		selected = new ArrayList<>();
		entity = new EntityBiServer();
		list = biServerManager.findByQuery("from EntityBiServer order by id");//BiPlatformManager.SERVER_LIST;
		allItmes = new LinkedHashMap<>();
		for (EntityBiChannel p : BiPlatformManager.CHANNEL_LIST) {
			allItmes.put(p.getName(), p.getId());
		}
		selected.add(0, new SelectObject());
		for (EntityBiServer s : list) {
			SelectObject st = new SelectObject();
			st.setSels(new ArrayList<String>());
			for (String c : s.getChannelIdList()) {
				st.getSels().add(c);
			}
			
			selected.add(st);
		}
	}

	public void search() {
		List<Object> cons = new ArrayList<>();
		StringBuffer hql = new StringBuffer(" from EntityBiServer l where 1=1 ");
		if (!Utils.isEmptyStr(entity.getId())) {
			hql.append(" and l.id = ? ");
			cons.add(entity.getId());
		}
		if (!Utils.isEmptyStr(entity.getName())) {
			hql.append(" and l.name = ? ");
			cons.add(entity.getName());
		}
		
		hql.append(" order by l.updateAt");
		
		list = biServerManager.findByQuery(hql.toString(), cons.toArray());
	}
	
	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, "0", entity, true, list)) {
			StringBuffer cs = new StringBuffer("");
			for (String cid : selected.get(0).getSels()) {
				if (BiPlatformManager.CHANNEL_MAP.get(cid) == null) {
					context.addMessage(null, new FacesMessage("所选渠道不存在：" + cid));
					return;
				}
				cs.append(cid).append(",");
			}
			entity.setChannelIds(cs.toString());
			biServerManager.persist(entity);
			
			context.addMessage(null, new FacesMessage("添加成功！"));
			biPlatformManager.init();
			initData();
		}
	}

	public void modify(EntityBiServer e, String index) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, index, e, false, list)) {
			StringBuffer cs = new StringBuffer("");
			for (String cid : selected.get(Integer.parseInt(index)).getSels()) {
				if (BiPlatformManager.CHANNEL_MAP.get(cid) == null) {
					context.addMessage(null, new FacesMessage("所选渠道不存在：" + cid));
					return;
				}
				
				cs.append(cid).append(",");
			}
			e.setChannelIds(cs.toString());
			biServerManager.merge(e);
			
			context.addMessage(null, new FacesMessage("修改成功！"));
			biPlatformManager.init();
			initData();
		}
	}
	
	public void delete(String id) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (!Utils.isEmptyStr(id)) {
			biServerManager.removeById(id);
		}
		
		context.addMessage(null, new FacesMessage("删除成功！"));
		biPlatformManager.init();
		initData();
	}

	private boolean validate(FacesContext context, String index, EntityBiServer ebp, boolean isAdd, List<EntityBiServer> ebpList) {
		if (Utils.isEmptyStr(ebp.getId())) {
			context.addMessage(null, new FacesMessage("区服ID不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getName())) {
			context.addMessage(null, new FacesMessage("区服名称不能为空！"));
			return false;
		}
		
		List<String> tmp = selected.get(Integer.parseInt(index)).getSels();
		if (tmp == null || tmp.size() <= 0) {
			context.addMessage(null, new FacesMessage("区服渠道不能为空！"));
			return false;
		}
		if (isAdd) {  // 如果是添加，则ID与名称皆不能重
			for (EntityBiServer e : ebpList) {
				if (ebp.getId().equals(e.getId())) {
					context.addMessage(null, new FacesMessage("区服ID重复！"));
					return false;
				}
				if (ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("区服名称重复！"));
					return false;
				}
			}
		} else {  // 如果是修改，则名称不能重
			for (EntityBiServer e : ebpList) {
				if (!ebp.getId().equals(e.getId()) && ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("区服名称重复！"));
					return false;
				}
			}
		}
		
		return true;
	}
	
	public class SelectObject {
		private List<String> sels;

		public List<String> getSels() {
			return sels;
		}

		public void setSels(List<String> sels) {
			this.sels = sels;
		}

		
	}
	
	public EntityBiServer getEntity() {
		return entity;
	}

	public void setEntity(EntityBiServer entity) {
		this.entity = entity;
	}

	public List<EntityBiServer> getList() {
		return list;
	}

	public void setList(List<EntityBiServer> list) {
		this.list = list;
	}
	
	public Map<String, String> getAllItmes() {
		return allItmes;
	}

	public void setAllItmes(Map<String, String> allItmes) {
		this.allItmes = allItmes;
	}
	public List<SelectObject> getSelected() {
		return selected;
	}

	public void setSelected(List<SelectObject> selected) {
		this.selected = selected;
	}
}
